package hot_100;

public class 删除链表的倒数第N个结点 {
    public static void main(String[] args) {

    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode r = head;
        ListNode l = new ListNode();
        l.next = head;
        ListNode befor = l;
        for (int i = 1; i < n; i++) {
            r = r.next;
        }
        while (r != null) {
            r = r.next;
            l = l.next;
        }
        if (befor.next != null)
            befor = befor.next.next;
        else
            befor = befor.next;
        return l.next;
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
